package com.ww.scm.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.ww.core.exception.ServiceException;
import com.ww.core.orm.Page;
import com.ww.core.orm.PropertyFilter;
import com.ww.core.utils.DateUtils;
import com.ww.scm.entity.mat.FinanceWastebook;
import com.ww.scm.entity.mat.FinanceAccount;
import com.ww.scm.constants.Constants; 
import com.ww.scm.entity.pub.Dictionary;
import com.ww.scm.repository.mat.FinanceAccountRepository;
import com.ww.scm.repository.mat.FinanceWastebookRepository;
import com.ww.scm.repository.pub.DictionaryRepository;
import com.ww.scm.repository.pub.SequenceRepository;
import com.ww.scm.valueObject.mat.FinanceAccountVo;
import com.ww.scm.valueObject.mat.FinanceWastebookVo;

//Spring Bean的标识.
@Component
// 默认将类中的所有函数纳入事务管理.
@Transactional
public class WastebookManager {
	/*
	 * 日志类
	 */
	private static Logger logger = LoggerFactory.getLogger(WastebookManager.class);

	/*
	 * 定义Repository
	 */
	private FinanceWastebookRepository financeWastebookRepository;
	private FinanceAccountRepository financeAccountRepository; 
	private DictionaryRepository dictionaryRepository;
	private SequenceRepository sequenceRepository;

	/*
	 * 注入Repository
	 */
	@Autowired
	public void setFinanceWastebookRepository(FinanceWastebookRepository financeWastebookRepository) {
		this.financeWastebookRepository = financeWastebookRepository;
	}
	@Autowired
	public void setFinanceAccountRepository(FinanceAccountRepository financeAccountRepository) {
		this.financeAccountRepository = financeAccountRepository;
	}
	@Autowired
	public void setDictionaryRepository(DictionaryRepository dictionaryRepository) {
		this.dictionaryRepository = dictionaryRepository;
	}
	@Autowired
	public void setSequenceRepository(SequenceRepository sequenceRepository) {
		this.sequenceRepository = sequenceRepository;
	}
	
	/*
	 * 业务方法
	 */
	
	/**
	 * 按属性过滤条件列表分页查找单据对象.
	 * @param page 分页参数.
	 * @param filters 查询参数
	 * 
	 * @return 分页查询结果, 附带结果列表及所有查询输入参数.
	 */
	@Transactional(readOnly = true)
	public Page<FinanceWastebook> findFinanceWastebook(Page<FinanceWastebook> page, List<PropertyFilter> filters) {
		return financeWastebookRepository.findPage(page, filters);
	}
	
	/**
	 * 查询单据对象按HQL分页查询.
	 * 
	 * @param page 分页参数.
	 * @param hql hql语句.
	 * @param values 命名参数,按名称绑定.
	 * 
	 * @return 分页查询结果, 附带结果列表及所有查询输入参数.
	 */
	@Transactional(readOnly = true)
	public Page<FinanceWastebook> findFinanceWastebook(Page<FinanceWastebook> page, String hql,Map<String, ?> values) {
		return financeWastebookRepository.findPage(page, hql, values);
	}

	/**
	 * 查询单据对象按HQL分页查询.
	 * 
	 * @param page 分页参数.
	 * @param hql hql语句.
	 * @param values 数量可变的查询参数,按顺序绑定.
	 * 
	 * @return 分页查询结果, 附带结果列表及所有查询输入参数.
	 */
	@Transactional(readOnly = true)
	public Page<FinanceWastebook> findFinanceWastebook(Page<FinanceWastebook> page, String hql, Object... values) {
		return financeWastebookRepository.findPage(page, hql, values);
	}

	/**
	 * 查询单据对象通过单据主键.
	 * 
	 * @param billId 单据主键.
	 * 
	 * @return 单据对象.
	 */
	@Transactional(readOnly = true)
	public FinanceWastebook findFinanceWastebookById(String financeWastebookId) {
		return financeWastebookRepository.get(financeWastebookId);
	}
	 
	
	public String randomBillCode(String billType) {
		Dictionary dictionary=dictionaryRepository.get(billType);
		StringBuffer codeBuffer=new StringBuffer();
		codeBuffer.append(dictionary.getCode());
		codeBuffer.append(Constants.LINE);
		codeBuffer.append(DateUtils.getCurrentDate());
		codeBuffer.append(Constants.LINE);
		int sequence=sequenceRepository.getNextSequence(billType);
		codeBuffer.append(sequence);
		return codeBuffer.toString();
	}	
	
	@Transactional(readOnly = true)
	public List<FinanceWastebook> findFinanceWastebook() {
		return financeWastebookRepository.getAll();
	}

	/**
	 * 按属性过滤条件列表查找单据对象列表.
	 * 
	 * @param filters 查询参数.
	 * 
	 * @return 单据对象列表.
	 */
	@Transactional(readOnly = true)
	public List<FinanceWastebook> findFinanceWastebookBy(List<PropertyFilter> filters) {
		return financeWastebookRepository.find(filters);
	}

	/**
	 * 按属性查找单据对象列表, 匹配方式为相等.
	 * 
	 * @param filters 查询参数.
	 * 
	 * @return 单据对象列表.
	 */	
	@Transactional(readOnly = true)
	public List<FinanceWastebook> findFinanceWastebookBy(String propertyName, String value) {
		return financeWastebookRepository.findBy(propertyName, value);
	}
	
	 
	
	/**
	 * 删除单据
	 * @param billId 单据主键
	 * @param operator 操作人主键
	 */
	public void deleteFinanceWastebook(String financeWastebookId, String operator) {
		FinanceWastebook financeWastebook = financeWastebookRepository.get(financeWastebookId);
		deleteFinanceWastebook(financeWastebook, operator);
	}
	
	/**
	 * 删除单据
	 * @param bill 单据对象
	 * @param operator 操作人主键
	 */
	public void deleteFinanceWastebook(FinanceWastebook financeWastebook, String operator) { 
		financeWastebookRepository.delete(financeWastebook, operator); 
	}
  
	/**
	 * 保存单据
	 * @param billVo 单据业务对象
	 */
	public FinanceWastebook saveFinanceWastebook(FinanceWastebookVo financeWastebookVo) {
		FinanceWastebook financeWastebook; 
		Date now=new Date();
		FinanceAccount account;
		if(StringUtils.isNotBlank(financeWastebookVo.getId())){
			//id不为空,是修改
			financeWastebook=financeWastebookRepository.get(financeWastebookVo.getId());
			if(financeWastebook==null){
				logger.error("不能找到主键为：'{}'单据.",financeWastebookVo.getId());
				throw new ServiceException("单据不能为空，不能找到主键："+financeWastebookVo.getId()+"单据.");
			}
			if(StringUtils.isNotBlank(financeWastebookVo.getAccountId())){
				account = financeAccountRepository.get(financeWastebookVo.getAccountId());
				financeWastebook.setAccount(account);
			} else{
				account=null;
			}
			
		}else{
			//id为空是新增
			financeWastebook=new FinanceWastebook();
			 
			if(StringUtils.isNotBlank(financeWastebookVo.getShopId())){
				Dictionary shop = dictionaryRepository.get(financeWastebookVo.getShopId());
				financeWastebook.setShop(shop);
			} 
			
			financeWastebook.setCreateTime(DateUtils.formatDateHms(now));
			financeWastebook.setSysFlag(Constants.SYS_FLAG_USEING);
			financeWastebook.setCreator(financeWastebookVo.getCreator());
			List<PropertyFilter> filters=new ArrayList<PropertyFilter>();
			filters.add(new PropertyFilter("EQS_financeForms.id",financeWastebookVo.getAccountTypeId()));
			filters.add(new PropertyFilter("EQS_dian.id",financeWastebookVo.getShopId()));
			account = financeAccountRepository.findUnique(filters);
			financeWastebook.setAccount(account);
		}
		
		
		
		
		
		if(account==null){
			logger.error("不能找到主键为：'{}'账号.",financeWastebookVo.getAccountId());
			throw new ServiceException("账号不能为空，不能找到主键："+financeWastebookVo.getAccountId()+"账号.");
		}
		
		if(StringUtils.isNotBlank(financeWastebookVo.getInoutTypeId())){
			Dictionary inouttype = dictionaryRepository.get(financeWastebookVo.getInoutTypeId());
			financeWastebook.setInoutType(inouttype);
		} 
		
		if(StringUtils.isNotBlank(financeWastebookVo.getInoutItemId())){
			Dictionary inoutItem = dictionaryRepository.get(financeWastebookVo.getInoutItemId());
			financeWastebook.setInoutItem(inoutItem);
		} 
		
		if(StringUtils.isNotBlank(financeWastebookVo.getInAccountItemId())){
			Dictionary inAccountItem = dictionaryRepository.get(financeWastebookVo.getInAccountItemId());
			financeWastebook.setInAccountItem(inAccountItem);
		} 
		
		if(StringUtils.isNotBlank(financeWastebookVo.getInAccountMethodId())){
			Dictionary inAccountMethod = dictionaryRepository.get(financeWastebookVo.getInAccountMethodId());
			financeWastebook.setInAccountMethod(inAccountMethod);
		} 
		
		if(StringUtils.isNotBlank(financeWastebookVo.getShopId())){
			Dictionary shop = dictionaryRepository.get(financeWastebookVo.getShopId());
			financeWastebook.setShop(shop);
		} 
		
		if(StringUtils.isNotBlank(financeWastebookVo.getAccountTypeId())){
			Dictionary accountType = dictionaryRepository.get(financeWastebookVo.getAccountTypeId());
			financeWastebook.setAccountType(accountType);
		} 
		financeWastebook.setInOutDate(financeWastebookVo.getInOutDate());
		financeWastebook.setMoney(financeWastebookVo.getMoney());
		financeWastebook.setAuto(financeWastebookVo.getAuto());
		financeWastebook.setRollbackFlag(financeWastebookVo.getRollbackFlag()); 
		financeWastebook.setRemark(financeWastebookVo.getRemark());
		financeWastebookRepository.save(financeWastebook);
		account.setMoney(account.getMoney()+financeWastebookVo.getMoney());
		
		financeAccountRepository.save(account);
		return financeWastebook;
	}



	
	
}
